<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-22
 * Time: 15:11
 */
namespace App\Http\Controllers\Mobile;
use App\Http\Controllers\Controller;
use App\Model\Backstage\Customers;
use App\Model\Mobile\MobilePlan;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;

class PlanTaskController extends Controller{

    /**
     * 首页任务模块
     */
    public function taskModule(Request $request)
    {
        if (!$request->input('hmp_user_id')) {
            return $this->error(Error::PARAM_MISSING);
        }

        $plan = new MobilePlan();
        $res = $plan->taskModule($request->input('hmp_user_id'));

        return $this->success('ok', $res);
    }
    /**
     * 移动端计划任务列表
     */
    public function plantasklist(Request $request)
    {
        $params = $request->except('apptoken');
        $validator = Validator::make($params, [
            'hmp_sign_type'          => 'required',//标记类型
            'hmp_user_id'            => 'required',//用户ID
            'authories'              => 'required',//权限
        ]);
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }

        $plan = new MobilePlan();
        $list = $plan->listPlan($params['hmp_user_id'], $params['hmp_sign_type'], $params['authories']);
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        $start_time = null;
        $end_time = strtotime(date('Y-m-d', time()).' 23:59:59');
        return $this->success('ok', $list, $start_time, $end_time);
    }

    /**
     * 添加计划任务
     */
    public function addtask(Request $request){
        $params = $request->except('apptoken');
        $validator = Validator::make($params, [
            'hmp_room_customer'      => 'required',//类型（1客源       2房源  ）
            'hmp_room_customer_id'   => 'required',// 房源或客源的ID
            'hmp_sign_type'          => 'required',//标记类型
            'hmp_user_id'            => 'required',//用户ID
            'hmp_user_department'    => 'required' //部门ID
        ]);
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }
        $customers = new Customers();
        $res = $customers->addplan($params);
        if (!$res) {
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '操作成功');

    }

    /**
     * 删除计划任务
     */
    public function delplantask(Request $request)
    {
        if (!$request->input('hmp_id')) {
            return $this->error(Error::PARAM_MISSING);
        }
        $plan = new MobilePlan();
        $res = $plan->delPlan($request->input('hmp_id'));
        if (!$res) {
            return $this->error(Error::DEL_ERROR);
        }
        return $this->success('ok', '删除成功');
    }
}